package com.lk.server.register;

import com.lk.server.annotations.Rpc;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 程序启动时
 *
 * @author kun.li
 */
@Component
@Slf4j
public class LocalRegisterScanner implements CommandLineRunner {
    String packageName = "com.lk.server";

    @Override
    public void run(String... args) throws Exception {
        Reflections ref = new Reflections(packageName);
        Set<Class<?>> set = ref.getTypesAnnotatedWith(Rpc.class);
        for (Class<?> aClass : set) {
            // 版本号和 beanName来区分多个实现类, 它们两个的关系时互斥的
            Rpc annotation = aClass.getAnnotation(Rpc.class);
            LocalRegister.register(aClass.getInterfaces()[0].getName(), annotation.version(), aClass.newInstance());
        }
    }
}
